"""
Case Type   : standby_statement_history
Case Name   : 传参个数为3,only_slow,T1和T2,三者入参顺序不同,only_slow值为非法值,部分步骤存在合理报错
Create At   : 2022/10/13
Owner       : @zou_jialiang050
Description :
    1.主机建表
    2.插入40W数据
    4.函数传参数量为3,only_slow在后,值为非法值,T2早于T1
    5.函数传参数量为3,only_slow在后,且为非法值,T1,T2为正常值
    6.函数传参数量为3,only_slow在后,且为非法值,T1为非法值.数字,T2为正常值
    7.函数传参数量为3,only_slow在后,且为非法值,T1为非法值.字符串,T2为正常值
    8.函数传参数量为3,only_slow在后,且为非法值,T1为非法值.其他时间类型,T2为正常值
    9.函数传参数量为3,only_slow在后,且为非法值,T1为错误的timestamp with time zone型.其他时间类型,T2为正常值
    10.函数传参数量为3,only_slow在后,且为非法值,T1为正常值,T2为非法值.数字
    11.函数传参数量为3,only_slow在后,且为非法值,T1为正常值,T2为非法值.字符串
    12.函数传参数量为3,only_slow在后,且为非法值,T1为正常值,T2为非法值.其他时间类型
    13.函数传参数量为3,only_slow在后,且为非法值,T1为正常值,T2为错误的timestamp with time zone型
    14.函数传参数量为3,only_slow在后,且为非法值,T2早于T1
    15.函数传参数量为3,only_slow在后,值为非法值,T1为正常值,T2为非法值.数字
    16.函数传参数量为3,only_slow在后,值为非法值,T1为正常值,T2为非法值.字符串
    17.函数传参数量为3,only_slow在后,值为非法值,T1为正常值,T2为非法值.其他时间类型
    18.函数传参数量为3,only_slow在后,值为非法值,T1为正常值,T2为错误的timestamp with time zone型
    19.清理环境
Expect      :
    1.建表成功
    2.插入数据成功
    3.查询成功
    4.失败
    5.失败
    6.失败
    7.失败
    8.失败
    9.失败
    10.失败
    11.失败
    12.失败
    13.失败
    14.失败
    15.失败
    16.失败
    17.失败
    18.失败
    19.成功
History     :
"""
import os
import time
import unittest

from yat.test import Node

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == primary_sh.get_node_num(),
                 'Single node, and subsequent codes are not executed.')
class StandbyStatementHistory(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.common = Common()
        self.table_name = 't_standby_statement_history_0017'
        self.enable_stmt = 'enable_stmt_track'
        self.enable_stmt_default = self.common.show_param(self.enable_stmt)

    def test_standby_statement_history(self):
        text = "-----前置条件:修改参数;expect:修改参数成功-----"
        self.logger.info(text)
        if self.enable_stmt_default != 'on':
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.enable_stmt}=on')
            self.assertTrue(result, '执行失败' + text)
        show_msg = self.common.show_param(self.enable_stmt)
        self.assertEquals(show_msg, 'on', '执行失败' + text)

        text = "-----step1:主机建表;expect:建表成功-----"
        self.logger.info(text)
        create_cmd = f"drop table if exists {self.table_name};" \
            f"create table {self.table_name}(id1 int);"
        create_msg = self.primary_sh.execut_db_sql(create_cmd)
        self.logger.info(create_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      create_msg, '执行失败' + text)

        text = "-----step2:插入40W数据;expect:插入数据成功-----"
        self.logger.info(text)
        insert_cmd = f"insert into {self.table_name} " \
            f"values (generate_series(1,400000));" \
            f"checkpoint;"
        insert_msg = self.primary_sh.execut_db_sql(insert_cmd)
        self.logger.info(insert_msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      insert_msg, '执行失败' + text)
        self.assertIn(self.constant.CHECKPOINT_SUCCESS_MSG,
                      insert_msg, '执行失败' + text)
        time.sleep(10)

        text = "-----step3:备机上查询数据;expect:查询成功-----"
        self.logger.info(text)
        sql_cmd = f"select count(*) from {self.table_name};"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('400000', sql_msg, '执行失败' + text)
        text = "----step4:函数传参数量为3,only_slow在后,值为非法值," \
               "T2早于T1; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', flse, '2022-06-08 19:68:28');"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step5:函数传参数量为3,only_slow在后,且为非法值," \
               "T1,T2为正常值; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28','2022-06-09 19:05:28',10);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step6:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为非法值:数字,T2为正常值; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"-1, '2022-06-09 19:05:28',000);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step7:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为非法值:字符串,T2为正常值; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'fadaf', '2022-06-09 19:05:28',000);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step8:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为非法值:其他时间类型,T2为正常值; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'10:05:28', '2022-06-09 19:05:28',1000);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step9:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为错误的timestamp with time zone型:其他时间类型,T2为正常值; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 198:05:28', '2022-06-09 19:05:28',dgdf);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step10:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为正常值,T2为非法值:数字; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', -1,45645);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step11:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为正常值,T2为非法值:字符串; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', 'dasfsd',0147327);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step12:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为正常值,T2为非法值:其他时间类型; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', '19:05:28',gdfgd0);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step13:函数传参数量为3,only_slow在后,且为非法值," \
               "T1为正常值,T2为错误的timestamp with time zone型; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', '2022-06-09 19:68:28',fdgdf1);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step14:函数传参数量为3,only_slow在后,且为非法值," \
               "T2早于T1; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', '2022-06-08 19:68:28',0sdfs);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step15:函数传参数量为3,only_slow在后,值为非法值," \
               "T1为正常值,T2为非法值:数字; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', fals,-1);"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step16:函数传参数量为3,only_slow在后,值为非法值," \
               "T1为正常值,T2为非法值:字符串; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', tru, 'dasfsd');"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step17:函数传参数量为3,only_slow在后,值为非法值," \
               "T1为正常值,T2为非法值:其他时间类型; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', fale, '19:05:28');"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)
        text = "----step18:函数传参数量为3,only_slow在后,值为非法值," \
               "T1为正常值,T2为错误的timestamp with time zone型; expect:失败----"
        self.logger.info(text)
        sql_cmd = f"select * from dbe_perf.standby_statement_history(" \
                  f"'2022-06-09 10:05:28', tue, '2022-06-09 19:68:28');"
        sql_msg = self.standby_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn('ERROR', sql_msg, '执行失败' + text)
        time.sleep(5)

    def tearDown(self):
        text_1 = "-----step8:清理环境;expect:清理环境成功-----"
        self.logger.info(text_1)
        drop_cmd = f"drop table {self.table_name} cascade;"
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        text_2 = "-----恢复参数;expect:恢复参数成功-----"
        self.logger.info(text_2)
        enable_stmt_current = self.common.show_param(self.enable_stmt)
        if self.enable_stmt_default != enable_stmt_current:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.enable_stmt}={self.enable_stmt_default}')
        show_enable_stmt = self.common.show_param(self.enable_stmt)
        self.logger.info("-----断言teardown-----")
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      drop_msg, '执行失败' + text_1)
        self.assertEquals(show_enable_stmt, self.enable_stmt_default,
                          '执行失败' + text_2)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
